package com.cskaoyan.service.category;

import com.cskaoyan.bean.MallCategory;
import com.cskaoyan.bean.MallCategoryExample;
import com.cskaoyan.mapper.MallCategoryMapper;
import com.cskaoyan.model.bo.category.CategoryDeleteBo;
import com.cskaoyan.model.bo.category.CategoryUpdateBo;
import com.cskaoyan.model.vo.BaseData;
import com.cskaoyan.model.vo.category.CategoryL1Vo;
import com.cskaoyan.model.vo.category.CategoryListChildrenVo;
import com.cskaoyan.model.vo.category.CategoryListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: mjm
 * @Date: 2021/11/30
 * @Time: 8:58
 * @Description:
 */
@Service
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    MallCategoryMapper mallCategoryMapper;

    @Override
    public BaseData categoryList() {
        MallCategoryExample example = new MallCategoryExample();
        //逻辑删除的类目不显示
        example.createCriteria().andDeletedEqualTo(false).andLevelEqualTo("L1");
        //查找所有的一级类目
        List<MallCategory> mallCategories = mallCategoryMapper.selectByExample(example);
        //创建VO列表
        ArrayList<CategoryListVo> categoryListVos = new ArrayList<>();
        //将查找到的一级类目属性封装到VO列表中
        for (MallCategory mallCategory : mallCategories) {

            CategoryListVo categoryListVo = new CategoryListVo();
            MallCategoryExample example1 = new MallCategoryExample();
            //逻辑删除的类目不显示
            example1.createCriteria().andDeletedEqualTo(false).andPidEqualTo(mallCategory.getId());

            //根据一级类目的id查找pid为id值的二级类目
            //逻辑删除的类目不显示
            Integer pid = mallCategory.getId();
            ArrayList<CategoryListChildrenVo> categoryListChildrenVos = mallCategoryMapper.selectByPid(pid);
            //List<MallCategory> mallCategories1 = mallCategoryMapper.selectByExample(example1);

            categoryListVo.setChildren(categoryListChildrenVos);
            categoryListVo.setDesc(mallCategory.getDesc());
            categoryListVo.setIconUrl(mallCategory.getIconUrl());
            categoryListVo.setId(mallCategory.getId());
            categoryListVo.setKeywords(mallCategory.getKeywords());
            categoryListVo.setLevel(mallCategory.getLevel());
            categoryListVo.setName(mallCategory.getName());
            categoryListVo.setPicUrl(mallCategory.getPicUrl());

            categoryListVos.add(categoryListVo);
        }

        long total = categoryListVos.size();
        return BaseData.list(categoryListVos,total);
    }

    @Override
    public MallCategory categoryCreate(MallCategory mallCategoryBo) {

        MallCategory mallCategory = new MallCategory();

        mallCategory.setDesc(mallCategoryBo.getDesc());
        mallCategory.setIconUrl(mallCategoryBo.getIconUrl());
        mallCategory.setKeywords(mallCategoryBo.getKeywords());
        mallCategory.setLevel(mallCategoryBo.getLevel());
        mallCategory.setName(mallCategoryBo.getName());
        mallCategory.setPicUrl(mallCategoryBo.getPicUrl());
        mallCategory.setPid(mallCategoryBo.getPid());
        mallCategory.setAddTime(new Date());
        mallCategory.setUpdateTime(new Date());
        mallCategory.setDeleted(false);

        int affctedRows = mallCategoryMapper.insertSelective(mallCategory);
        if(affctedRows != 1){
            return null;
        }
        Integer id = mallCategory.getId();
        MallCategory mallCategoryVo = mallCategoryMapper.selectByPrimaryKey(id);
        return mallCategoryVo;
    }

    @Override
    public int categoryUpdate(CategoryUpdateBo categoryUpdateBo) {
        MallCategory mallCategory = new MallCategory();

        mallCategory.setDesc(categoryUpdateBo.getDesc());
        mallCategory.setIconUrl(categoryUpdateBo.getIconUrl());
        mallCategory.setKeywords(categoryUpdateBo.getKeywords());
        mallCategory.setLevel(categoryUpdateBo.getLevel());
        mallCategory.setName(categoryUpdateBo.getName());
        mallCategory.setPicUrl(categoryUpdateBo.getPicUrl());
        mallCategory.setPid(categoryUpdateBo.getPid());
        mallCategory.setUpdateTime(new Date());

        MallCategoryExample example = new MallCategoryExample();
        example.createCriteria().andIdEqualTo(categoryUpdateBo.getId());
        //根据id进行更新编辑
        int affectedRows = mallCategoryMapper.updateByExampleSelective(mallCategory,example);
        return affectedRows;
    }

    @Override
    public int categoryDelete(CategoryDeleteBo categoryDeleteBo) {
        MallCategory mallCategory = new MallCategory();

        mallCategory.setDesc(categoryDeleteBo.getDesc());
        mallCategory.setIconUrl(categoryDeleteBo.getIconUrl());
        mallCategory.setKeywords(categoryDeleteBo.getKeywords());
        mallCategory.setLevel(categoryDeleteBo.getLevel());
        mallCategory.setName(categoryDeleteBo.getName());
        mallCategory.setPicUrl(categoryDeleteBo.getPicUrl());
        mallCategory.setUpdateTime(new Date());
        mallCategory.setDeleted(true);

        //逻辑删除，实则更新
        MallCategoryExample example = new MallCategoryExample();
        example.createCriteria().andIdEqualTo(categoryDeleteBo.getId());
        int affectedRows = mallCategoryMapper.updateByExampleSelective(mallCategory, example);
        return affectedRows;
    }

    @Override
    public BaseData categoryl1() {
        ArrayList<CategoryL1Vo> categoryL1Vos = mallCategoryMapper.selectcategoryl1();
        return BaseData.list(categoryL1Vos,categoryL1Vos.size());
    }
}
